package hoang.hut.app.network;

import hoang.hut.app.info.WebserviceConfig;

import java.io.IOException;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;

import android.content.Context;


/**
 * AsyncHttpGet makes http post request based on AsyncTask
 * 
 * @author sonnv6207
 */
public class AsyncHttpPost extends AsyncHttpBase
{
    /**
     * Constructor
     * 
     * @param context
     * @param listener
     * @param parameters
     */
    public AsyncHttpPost(Context context, AsyncHttpResponseListener listener,
        List<NameValuePair> parameters)
    {
        super(context, listener, parameters);
    }

	/**
     * Constructor
     * 
     * @param context
     * @param process
     * @param parameters
     */
    public AsyncHttpPost(Context context, AsyncHttpResponseProcess process,
        List<NameValuePair> parameters)
    {
        super(context, process, parameters);
    }
    
    @Override
    protected String request(String url)
    {
        try
        {
            HttpParams params = new BasicHttpParams();
            HttpConnectionParams.setConnectionTimeout(params, WebserviceConfig.NETWORK_TIME_OUT);
            HttpConnectionParams.setSoTimeout(params, WebserviceConfig.NETWORK_TIME_OUT);
            HttpClient httpclient = createHttpClient(url, params);
            
            HttpPost httppost = new HttpPost(url);
            
            httppost.setEntity(new UrlEncodedFormEntity(parameters));

            try
            {
                response = httpclient.execute(httppost);
                statusCode = NETWORK_STATUS_OK;
                CAUSE_ERROR = "";
            }
            catch(java.net.SocketTimeoutException ex)
            {
                statusCode = NETWORK_STATUS_TIMEOUT;
            }
            catch(javax.net.ssl.SSLException ex)
            {
                statusCode = NETWORK_STATUS_ERROR;
                ex.printStackTrace();
            }
            catch(org.apache.http.conn.ConnectTimeoutException timeoutEx){
                statusCode = NETWORK_STATUS_TIMEOUT;
                timeoutEx.printStackTrace();
            }
            catch(ClientProtocolException clientEx){
                statusCode = NETWORK_STATUS_ERROR;
                clientEx.printStackTrace();
            }
            catch(IOException ioEx){
                statusCode = NETWORK_STATUS_ERROR;
                ioEx.printStackTrace();
            }
            
        }
        catch (Exception e)
        {
            statusCode = NETWORK_EXCEPTION;
            CAUSE_ERROR = e.getMessage();
            e.printStackTrace();
        }
        return null;
    }
}
